
* REPLICATION CODE: JENNINGS & WLEZIEN, 'THE COMPARATIVE TIMELINE OF ELECTIONS', AMERICAN JOURNAL OF POLITICAL SCIENCE *

* FIGURE S3. Government Control of Election Timing (Germany, Netherlands, Norway, Spain, US vs. Australia, Canada, Ireland, NZ, Portugal, UK)

*********************************************************************************************
** MULTIPLE IMPUTATION/BOOTSTRAP - ALL ELECTIONS - VOTE = POLLS + PARTY *********************
*********************************************************************************************

use LONG_MI.dta, clear

keep if election=="Legislative"
keep if govcontrol==1

drop if daysbeforeED>200
gen _parXcou_ = countryid+(partyid/100)

gen rmse=.
gen se_rmse=.

foreach x of numlist 1/200 {
quietly mi estimate, cmdok: mybstrprmseareg vote_ poll_ if daysbeforeED==`x' & pollcycle>199
matrix bts = e(b_mi)
scalar bt = bts[1,1]
replace rmse=bt in `x'
matrix bts = e(V_mi)
scalar bt = bts[1,1]
replace se_rmse=sqrt(bt) in `x'
ereturn clear
}

*********************************************************************************************
** OUTPUT/FORMAT THE TIMELINE DATASET *******************************************************
*********************************************************************************************

keep rmse se_rmse
gen daysbeforeED=_n

drop if rmse == .
mi xtset, clear 
sort daysbeforeED

gen rmse_min95=rmse+se_rmse
gen rmse_max95=rmse-se_rmse

save MI_P_ALL_ED200_RMSE_LEG_ELECT_A.dta, replace

*********************************************************************************************
** MULTIPLE IMPUTATION/BOOTSTRAP - ALL ELECTIONS - VOTE = POLLS + PARTY *********************
*********************************************************************************************

use LONG_MI.dta, clear

keep if election=="Legislative"
keep if govcontrol==0

drop if daysbeforeED>200
gen _parXcou_ = countryid+(partyid/100)

gen rmse=.
gen se_rmse=.

foreach x of numlist 1/200 {
quietly mi estimate, cmdok: mybstrprmseareg vote_ poll_ if daysbeforeED==`x' & pollcycle>199
matrix bts = e(b_mi)
scalar bt = bts[1,1]
replace rmse=bt in `x'
matrix bts = e(V_mi)
scalar bt = bts[1,1]
replace se_rmse=sqrt(bt) in `x'
ereturn clear
}

*********************************************************************************************
** OUTPUT/FORMAT THE TIMELINE DATASET *******************************************************
*********************************************************************************************

keep rmse se_rmse
gen daysbeforeED=_n

drop if rmse == .
mi xtset, clear 
sort daysbeforeED

gen rmse_min95=rmse+se_rmse
gen rmse_max95=rmse-se_rmse

save MI_P_ALL_ED200_RMSE_LEG_ELECT_B.dta, replace

****** SUPPLEMENTARY FIGURE S3.3 **************************************************************
*Root Mean Square Errors for Legislative Elections with Government Control of Election Timing vs. No Control
*Germany, Netherlands, Norway, Spain, US vs. Australia, Canada, Ireland, NZ, Portugal, UK 

use MI_P_ALL_ED200_RMSE_LEG_ELECT_A.dta, clear 
rename rmse elect0_rmse
rename rmse_min95 elect0_rmse_min95
rename rmse_max95 elect0_rmse_max95
drop se_rmse
sort daysbeforeED
save MI_P_ALL_ED200_RMSE_LEG_ELECT_A.dta, replace

use MI_P_ALL_ED200_RMSE_LEG_ELECT_B.dta, clear 
rename rmse elect1_rmse
rename rmse_min95 elect1_rmse_min95
rename rmse_max95 elect1_rmse_max95
drop se_rmse
sort daysbeforeED
save MI_P_ALL_ED200_RMSE_LEG_ELECT_B.dta, replace

merge daysbeforeED using MI_P_ALL_ED200_RMSE_LEG_ELECT_A.dta

* 
twoway /*
*/  || line elect1_rmse daysbeforeED if daysbeforeED<200, /*
*/  clpattern(solid) clcolor(black) clwidth(medthick)  /* 
*/  || line elect0_rmse daysbeforeED if daysbeforeED<200, /*
*/  clpattern(dash) clcolor(black) clwidth(medthick)  /* 
*/  || line elect1_rmse_min95 daysbeforeED if daysbeforeED<200,  /*
*/  clpattern(dot) clcolor(black) clwidth(medium) /*
*/  || line elect1_rmse_max95 daysbeforeED if daysbeforeED<200,  /*
*/  clpattern(dot) clcolor(black) clwidth(medium) /*
*/  || line elect0_rmse_min95 daysbeforeED if daysbeforeED<200,  /*
*/  clpattern(dot) clcolor(black) clwidth(medium) /*
*/  || line elect0_rmse_max95 daysbeforeED if daysbeforeED<200,  /*
*/  clpattern(dot) clcolor(black) clwidth(medium) /*
*/  , scheme(s2mono) graphregion(color(white)) /*
*/  xtitle("Days Until Election") /*
*/  title("") /*
*/  ylabel(0(1)10, gmax angle(horizontal)) /*
*/  ytick(0(1)10) /*
*/  ytitle(Root Mean Squared Error) /*
*/  xlabel(0(50)200) /*
*/  xscale(reverse) xscale(titlegap(2)) /*
*/  legend(order(1 "Control of Election Timing" 2 "No Control of Election Timing") /*
*/  rows(1) /*
*/  size(medsmall))

graph export FigS3.tif
